package com.jzcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzcn.common.base.BaseServiceImpl;
import com.jzcn.common.dto.common.KeyWordDTO;
import com.jzcn.common.dto.common.LabelValueDTO;
import com.jzcn.common.dto.system.role.RoleDTO;
import com.jzcn.common.dto.system.role.RoleListDTO;
import com.jzcn.common.dto.system.role.SearchRoleDTO;
import com.jzcn.common.enums.BaseStatusEnum;
import com.jzcn.common.exceptions.BusinessException;
import com.jzcn.common.utils.BeanUtils;
import com.jzcn.common.utils.JzPage;
import com.jzcn.mapper.SysRoleMapper;
import com.jzcn.mapper.SysRoleResourcesMapper;
import com.jzcn.domain.SysRole;
import com.jzcn.domain.SysRoleResources;
import com.jzcn.service.SysRoleResourcesService;
import com.jzcn.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    SysRoleResourcesService sysRoleResourcesService;

    @Resource
    SysRoleResourcesMapper sysRoleResourcesMapper;

    @Override
    public JzPage<RoleListDTO> listPage(KeyWordDTO dto) {
        IPage<SysRole> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(dto.getKeyword())){
            wrapper.like(SysRole::getName,dto.getKeyword());
        }
        if(dto.getStatus()!=null){
            wrapper.eq(SysRole::getName,dto.getStatus());
        }

        JzPage<RoleListDTO> page = BeanUtils.convertPage(new JzPage<>(baseMapper.selectPage(iPage,wrapper)), RoleListDTO.class);
        List<SysRoleResources> roleResourcessList = sysRoleResourcesMapper.listAll();
        Map<Integer,List<Integer>> map = roleResourcessList.stream().collect(Collectors.groupingBy(SysRoleResources::getRoleId,Collectors.mapping(e-> e.getResourcesId().intValue(),Collectors.toList())));
        page.getList().forEach(e->{
            List<Integer> resourcesIds = map.get(e.getId());
            e.setResourcesIds(resourcesIds);
        });
        return page;
    }

    @Override
    public List<LabelValueDTO> listData() {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getStatus, BaseStatusEnum.START.getValue());
        List<RoleListDTO> list = BeanUtils.convertList(baseMapper.selectList(wrapper),RoleListDTO.class);
        List<LabelValueDTO> labelValues = new ArrayList<>();
        list.forEach(e->{
            LabelValueDTO labelValue = new LabelValueDTO();
            labelValue.setValue(e.getId().toString());
            labelValue.setLabel(e.getName());
            labelValues.add(labelValue);
        });
        return labelValues;
    }

    @Override
    public boolean delete(Integer id) {
        return super.remove(id);
    }

    @Override
    public boolean update(RoleDTO roleDTO, Integer id) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleDTO,sysRole);
        sysRole.setId(id);
        if(super.update(sysRole)){
            sysRoleResourcesService.remove(new LambdaQueryWrapper<SysRoleResources>().eq(SysRoleResources::getRoleId, sysRole.getId()));
            saveBatchResources(roleDTO,sysRole.getId());
            return true;
        }
        return false;
    }

    @Override
    public boolean roleStatus(Integer id, Integer status) {
        SysRole sysRole = super.getById(id);
        sysRole.setStatus(status);
        return this.update(sysRole);
    }

    @Override
    public boolean create(RoleDTO roleDTO) {
        if(super.checkUniqueness("name",roleDTO.getName())){
            throw new BusinessException("角色名称已存在");
        }
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(roleDTO,sysRole);
        if(super.create(sysRole)){
            saveBatchResources(roleDTO,sysRole.getId());
            return true;
        }
        return false;
    }


    public void saveBatchResources(RoleDTO roleDTO,Integer roleId){
        List<SysRoleResources> sysRoleResourcesAll = new ArrayList<>();
        for(Integer v: roleDTO.getResourcesIds()){
            SysRoleResources sysRoleResources = new SysRoleResources();
            sysRoleResources.setResourcesId(v);
            sysRoleResources.setRoleId(roleId);
            sysRoleResourcesAll.add(sysRoleResources);
        }
        sysRoleResourcesService.saveBatch(sysRoleResourcesAll);
    }



}
